package binary_tree

import (
	"container/list"
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

func GenerateEmployee(level, maxLevel, maxHappy, maxNext int) *Employee {
	rand.Seed(time.Now().UnixNano())
	if level > maxLevel || rand.Intn(10) < 4 {
		return &Employee{happy: rand.Intn(maxHappy+1), nexts: make([]*Employee, 0)}
	}
	node := &Employee{happy: rand.Intn(maxHappy+1), nexts: make([]*Employee, 0)}
	for i:=0; i<rand.Intn(maxNext+1); i++ {
		node.nexts = append(node.nexts, GenerateEmployee(level+1, maxLevel, maxHappy, maxNext))
	}
	return node
}

type EmployeePrint struct {
	emp *Employee
	parent *Employee
}

func PrintEmployee(boss *Employee) {
	if boss == nil {return}
	queue := list.New()
	queue.PushBack(EmployeePrint{emp:boss})
	var lastPSP *Employee
	for queue.Len() > 0 {
		curSize := queue.Len()
		ans := ""
		for i:=0; i<curSize; i++ {
			p := queue.Remove(queue.Front()).(EmployeePrint)
			if lastPSP == p.parent {
				ans += strconv.Itoa(p.emp.happy) + " "
			} else {
				ans += fmt.Sprintf("(%d)", p.parent.happy) + strconv.Itoa(p.emp.happy) + " "
			}
			lastPSP = p.parent
			for _, next := range p.emp.nexts {
				queue.PushBack(EmployeePrint{next, p.emp})
			}
		}
		fmt.Println(ans)
	}

}